﻿<%@ WebHandler Language="C#" Class="dialog" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Collections.Generic;
using jsm.SqlHelper;
using dbcaac;

public class dialog : IHttpHandler
{
    public void ProcessRequest (HttpContext context) {

        string sqlOld = string.Format(@"SELECT jyName, jyContent, jyStandard, jyBasis, jyPenalizeRef, 
                                        (CASE WHEN jyIsMust=1 THEN '是' ELSE '否' END) AS jyIsMust 
                                        FROM [jiyuanNR] WHERE jyId = {0} AND jyNewFlag <> 1", context.Request["id"]);
        string sqlNew = string.Format(@"SELECT jyrzName, jyrzContent, jyrzStandard, jyrzBasis, jyrzPenalizeRef, jyrzChangedType, 
                                        (CASE WHEN jyrzIsMust=1 THEN '是' ELSE '否' END) AS jyrzIsMust 
                                        FROM [jiyuanNRLog] WHERE jyrzLogId = '{0}'", context.Request["logId"]);

        SqlDataReader oldContent = SqlServerHelper.ExecuteReader(sqlOld);
        SqlDataReader newContent = SqlServerHelper.ExecuteReader(sqlNew);

        string templateTable = @"<table cellpadding='0' cellspacing='0'>
                                    <tr><th style=""white-space: nowrap"" width=""100"">检查内容</th>
                                        <td style=""word-break:break-all"" width=""500"">{0}</td></tr>
                                    <tr><th>检查方法</th><td style=""word-break:break-all"" width=""500"">{1}</td></tr>
                                    <tr><th>检查标准</th><td style=""word-break:break-all"" width=""500"">{2}</td></tr>
                                    <tr><th>依据</th><td>{3}</td style=""word-break:break-all"" width=""500""></tr>
                                    <tr><th>处罚参考</th><td>{4}</td style=""word-break:break-all"" width=""500""></tr>
                                    <tr><th>必选项目</th><td>{5}</td style=""word-break:break-all"" width=""500""></tr>                                    
                                </table>";

        string blankTable = string.Format(templateTable, "", "", "", "", "", "");

        string chgType = string.Empty;
        string newTable = string.Empty;
        string oldTable = string.Empty;
        
        if (oldContent.Read())
        {
            oldTable = string.Format(
                                    templateTable, 
                                    oldContent.GetSqlValue(oldContent.GetOrdinal("jyName")).ToString().Replace("\"", "\"\""),
                                    oldContent.GetSqlValue(oldContent.GetOrdinal("jyContent")).ToString().Replace("\"", "\"\""),
                                    oldContent.GetSqlValue(oldContent.GetOrdinal("jyStandard")).ToString().Replace("\"", "\"\""),
                                    oldContent.GetSqlValue(oldContent.GetOrdinal("jyBasis")).ToString().Replace("\"", "\"\""),
                                    oldContent.GetSqlValue(oldContent.GetOrdinal("jyPenalizeRef")).ToString().Replace("\"", "\"\""),
                                    oldContent.GetSqlValue(oldContent.GetOrdinal("jyIsMust")).ToString()
                                    );
        }

        if (newContent.Read())
        {
            chgType = newContent.GetSqlValue(newContent.GetOrdinal("jyrzChangedType")).ToString();
            newTable = string.Format(
                                    templateTable, 
                                    newContent.GetSqlValue(newContent.GetOrdinal("jyrzName")).ToString().Replace("\"", "\"\""),
                                    newContent.GetSqlValue(newContent.GetOrdinal("jyrzContent")).ToString().Replace("\"", "\"\""),
                                    newContent.GetSqlValue(newContent.GetOrdinal("jyrzStandard")).ToString().Replace("\"", "\"\""),
                                    newContent.GetSqlValue(newContent.GetOrdinal("jyrzBasis")).ToString().Replace("\"", "\"\""),
                                    newContent.GetSqlValue(newContent.GetOrdinal("jyrzPenalizeRef")).ToString().Replace("\"", "\"\""),
                                    newContent.GetSqlValue(newContent.GetOrdinal("jyrzIsMust")).ToString()
                                   );
        }

        context.Response.Write(@"<div class=""diff"">");
        context.Response.Write(@"<h2>差异比较（绿色代表新增的内容，红色代表删除的内容）：</h2>");        
        HtmlDiff htmlDiff = null;
        if ("DELETE" == chgType)
        {
            htmlDiff = new HtmlDiff(oldTable, blankTable);
        }
        else if ("NEW" == chgType)
        {
            htmlDiff = new HtmlDiff(blankTable, newTable);
        }
        else
        {
            htmlDiff = new HtmlDiff(oldTable, newTable);
        }
        
        context.Response.Write(htmlDiff.Build());

        if ("DELETE" == chgType)
        {
            context.Response.Write(@"<br /><h2 style=""color:#F00"">警告：在删除本节点的同时，其所有子节点也将被一起删除！</h2>");
        }
        
        context.Response.Write(@"</div>");

        context.Response.End(); 
    } 
    
    public bool IsReusable {
        get {
            return false;
        }
    }
}